﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QuanLyQuanCafe.BusinessLogic;
using QuanLyQuanCafe.BusinessObject;


namespace QuanLyQuanCafe
{
    public partial class FrmMonAn : Form
    {
        private NhomMonBL nhommonBL = new NhomMonBL();
        private MonAnBL monanBL = new MonAnBL();
        private NguyenLieuBL nguyenlieuBL = new NguyenLieuBL();
        private CT_MonAnBL ctmonanBL = new CT_MonAnBL();
        private DonViTinhBL dvtBL = new DonViTinhBL();
        private int manl;
        public FrmMonAn()
        {
            InitializeComponent();
            cbNhomMon.DataSource = nhommonBL.DSNhomMon();
            cbNhomMon.DisplayMember = "TenNhom";
            cbNhomMon.ValueMember = "MaNhom";
            cbNL.DataSource = nguyenlieuBL.DSNguyenLieu();
            cbNL.DisplayMember = "TenNL";
            cbNL.ValueMember = "MaNL";
            cbDVT.DataSource = dvtBL.LayDanhSachDVT();
            cbDVT.DisplayMember = "TenDVT";
            cbDVT.ValueMember = "MaDVT";

        }

        private void FrmMonAn_Load(object sender, EventArgs e)
        {
            dataGridView1.DataSource = monanBL.DSMonNhomMon();
            AnHienButton(false);
            btCongThuc.Enabled = false;
        }

        private void AnHienButton(bool flag)
        {
            btnThem.Enabled = !flag;
            btnSua.Enabled = !flag;
            btnXoa.Enabled = !flag;
            btnLuu.Enabled = flag;
            btnHuy.Enabled = flag;
            btCongThuc.Enabled = false;

            tbMaMon.Enabled = flag;
            tbTenMon.Enabled = flag;
            cbNhomMon.Enabled = flag;
            tbGia.Enabled = flag;
            dataGridView1.Enabled = !flag;
        }
        
        private void CapNhatText()
        {
            tbMaMon.Text = "";
            tbTenMon.Text = "";
            tbGia.Text = "";
        }


        private void dataGridView1_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            int row = e.RowIndex;
            try
            {
                tbMaMon.Text = dataGridView1.Rows[row].Cells["MaMon"].Value.ToString();
                tbTenMon.Text = dataGridView1.Rows[row].Cells["TenMon"].Value.ToString();
                tbGia.Text = dataGridView1.Rows[row].Cells["GiaThuong"].Value.ToString();
                cbNhomMon.Text = dataGridView1.Rows[row].Cells["TenNhom"].Value.ToString();
            }
            catch
            {

            }
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            AnHienButton(true);
            CapNhatText();
            tbMaMon.Enabled = false;
            int mamon = monanBL.FindMa();
            tbMaMon.Text = mamon.ToString();
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            AnHienButton(true);
            tbMaMon.Enabled = false;
            btCongThuc.Enabled = true;
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn Có Muốn Xóa Món [" + tbTenMon.Text + "] Không?", "Quản Lý Quán Cafe", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                if (monanBL.XoaMon(tbMaMon.Text))
                    FrmMonAn_Load(sender, e);
        }
        private MonAnBO LayThongTinMon()
        {
            MonAnBO monanBO = new MonAnBO();
            monanBO.MaMon = int.Parse(tbMaMon.Text);
            monanBO.TenMon = tbTenMon.Text;
            monanBO.GiaThuong = float.Parse(tbGia.Text);
            monanBO.MaNhom = int.Parse(cbNhomMon.SelectedValue.ToString());
            return monanBO;
        }
        private void btnLuu_Click(object sender, EventArgs e)
        {
            try
            {
                MonAnBO monanBO = LayThongTinMon();
                if (monanBL.CheckMonAn(monanBO.MaMon) )//&& ctmonanBL.CheckCTMonAn(int.Parse(tbMaMon.Text)))
                {
                    if (monanBL.SuaMon(monanBO))
                        FrmMonAn_Load(sender, e);
                }
                else //if (ctmonanBL.CheckCTMonAn(int.Parse(tbMaMon.Text)))
                {
                    if (monanBL.ThemMon(monanBO))
                        FrmMonAn_Load(sender, e);
                }
            }
            catch
            {
                MessageBox.Show("Kiểm Lại Các Thông Tin Đã Nhập.", "Quán Lý Quán Cafe", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            AnHienButton(false);
            if (!ctmonanBL.CheckCTMonAn(int.Parse(tbMaMon.Text)))
            {
                if (ctmonanBL.XoaCTMaMon(tbMaMon.Text))
                    FrmMonAn_Load(sender, e);
            }
        }


        private void AnHienButtonCt(bool flag)
        {
            btThemCT.Enabled = !flag;
            btSuaCT.Enabled = !flag;
            btXoaCT.Enabled = !flag;
            btLuuCT.Enabled = flag;
            btHuyCT.Enabled = flag;
            cbDVT.Enabled = flag;
            cbNL.Enabled = flag;
            tbSoLuong.Enabled = flag;
            dataGridView2.Enabled = !flag;
        }
        

        private void dataGridView2_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            int row = e.RowIndex;
            try
            {
                tbSoLuong.Text = dataGridView2.Rows[row].Cells["SoLuong"].Value.ToString();
                cbNL.Text = dataGridView2.Rows[row].Cells["TenNL"].Value.ToString();
                cbDVT.Text = dataGridView2.Rows[row].Cells["TenDVT"].Value.ToString();
            }
            catch
            {

            }
        }

        private void btThemCT_Click(object sender, EventArgs e)
        {
            AnHienButtonCt(true);
            tbSoLuong.Text="";
            manl = 0;
        }

        private void btSuaCT_Click(object sender, EventArgs e)
        {
            AnHienButtonCt(true);
            btCongThuc.Visible = true;
            manl = int.Parse(cbNL.SelectedValue.ToString());
        }

        private void btXoaCT_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn Có Muốn Bỏ Nguyên Liệu [" + cbNL.Text + "] Ra Khỏi Món ["+tbTenMon.Text+"] Không?", "Quản Lý Quán Cafe", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                if (ctmonanBL.XoaCTMon(cbNL.SelectedValue.ToString(), tbMaMon.Text))
                {
                    dataGridView2.DataSource = ctmonanBL.DSMonAnNguyenLieu(int.Parse(tbMaMon.Text));
                    AnHienButtonCt(false);
                }
        }
        private CT_MonAnBO LayThongTinCTMon()
        {
            CT_MonAnBO ctmonanBO = new CT_MonAnBO();
            ctmonanBO.MaMon = int.Parse(tbMaMon.Text);
            ctmonanBO.SoLuong = float.Parse(tbSoLuong.Text);
            ctmonanBO.MaNL = int.Parse(cbNL.SelectedValue.ToString());
            ctmonanBO.MaDVT = int.Parse(cbDVT.SelectedValue.ToString());
            return ctmonanBO;
        }

        public bool testNL(int value1, int value2)
        {
            if (value1 == value2)
                return true;
            else return false;
        }


        private void btLuuCT_Click(object sender, EventArgs e)
        {
            try
            {
                CT_MonAnBO ctmonanBO = LayThongTinCTMon();
                if (ctmonanBL.CheckCTMonAn(ctmonanBO.MaMon) )
                {
                    if (manl != 0)
                    {
                        if (testNL(manl, int.Parse(cbNL.SelectedValue.ToString())))
                        {
                            if (ctmonanBL.SuaCTMonNL(ctmonanBO))
                            {
                                dataGridView2.DataSource = ctmonanBL.DSMonAnNguyenLieu(int.Parse(tbMaMon.Text));
                                AnHienButtonCt(false);
                            }
                        }
                        else
                        {
                            if (ctmonanBL.SuaCTMon(ctmonanBO))
                            {
                                dataGridView2.DataSource = ctmonanBL.DSMonAnNguyenLieu(int.Parse(tbMaMon.Text));
                                AnHienButtonCt(false);
                            }
                        }
                    }
                    else
                    {
                        if (ctmonanBL.ThemCTMon(ctmonanBO))
                        {
                            dataGridView2.DataSource = ctmonanBL.DSMonAnNguyenLieu(int.Parse(tbMaMon.Text));
                            AnHienButtonCt(false);
                        }
                    }
                }
                else
                {
                    if (ctmonanBL.ThemCTMon(ctmonanBO))
                    {
                        dataGridView2.DataSource = ctmonanBL.DSMonAnNguyenLieu(int.Parse(tbMaMon.Text));
                        AnHienButtonCt(false);
                    }
                }

            }
            catch
            {
                MessageBox.Show("Kiểm Lại Các Thông Tin Đã Nhập.", "Quán Lý Quán Cafe", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void btHuyCT_Click(object sender, EventArgs e)
        {
            AnHienButtonCt(false);
        }

        private void btOK_Click(object sender, EventArgs e)
        {            
            panel1.Visible = false;
        }

        private void btCongThuc_Click(object sender, EventArgs e)
        {
            panel1.Visible = true;
            dataGridView2.DataSource = ctmonanBL.DSMonAnNguyenLieu(int.Parse(tbMaMon.Text));
            AnHienButtonCt(false);
        }
        
    }
}
